/* xerror.c -- Camada de abstração para chamada de erros
 *
 * Copyright (C) 2007, Diogo F. S. Ramos <diogofsr at gmail.com>
 * GEAR - Grupo de Estudos Avançados em Robótica
 * <http://www.sel.eesc.usp.br/gear>
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 * 
*/

#define _GNU_SOURCE 1

#include <string.h>
#include <stdlib.h>
#include <stdio.h>

//#include "xerror.h"

static void error_message( int exit_status, const char *mode, const char *message );
static const char *program_name = NULL;

static void
error_message( int exit_status, const char *mode, const char *message )
{
    fprintf( stderr, "%s: %s: %s.\n", program_name, mode, message );

    if( exit_status >= 0 )
    {
	exit(exit_status);
    }

    return;
}

void
xerror_set_program_name (const char *path)
{
  if (!program_name)
      program_name = strdup ((const char*)basename (path));
}

void
xerror_warning( const char *message )
{
    error_message( -1, "warning", message );

    return;
}

void
xerror_error( const char *message )
{
    error_message( -1, "error", message );

    return;
}

void
xerror_fatal( const char *message )
{
    error_message( EXIT_FAILURE, "FATAL", message );

    return;
}
